package com.lzm.listendemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.webkit.WebView;
import android.widget.Button;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzm.listendemo.adapter.ImageBannerAdapter;
import com.lzm.listendemo.adapter.MyAdapter;
import com.lzm.listendemo.bean.BannerBean;
import com.lzm.listendemo.bean.WxArticle;
import com.lzm.listendemo.util.HttpUtil;
import com.lzm.listendemo.view.dialog.NotifyDialog;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainTestActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private Banner banner;

    private MyAdapter adapter;

    private List<WxArticle> wxArticles;
    private List<BannerBean> bannerBeans;
    private ImageBannerAdapter imageBannerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_test);
        wxArticles = new ArrayList<>();
        bannerBeans = new ArrayList<>();
        adapter = new MyAdapter(this,wxArticles);
        banner = findViewById(R.id.banner);
        banner.addBannerLifecycleObserver(this);
        imageBannerAdapter = new ImageBannerAdapter(this,bannerBeans);
        banner.setAdapter(imageBannerAdapter);
        recyclerView = findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setItemOnClick((view, item) -> {
            NotifyDialog dialog = new NotifyDialog(MainTestActivity.this,R.layout.layout_full_screen_web, Gravity.BOTTOM);
            WebView webView = dialog.findById(R.id.web);
            /*webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });*/
            dialog.findById(R.id.btn_back).setOnClickListener(btn->{
                dialog.dismiss();
            });

            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(item.getLink());
            dialog.setAutoDismiss(false);
            dialog.setCancelable(true);
            dialog.show();
        });
        Button btn = findViewById(R.id.to_second);
        btn.setOnClickListener(v -> {
            Intent intent = new Intent(this, SecondTestActivity.class);
            startActivity(intent);

        });
        btn.setOnLongClickListener(v -> false);
        initData();
    }

    private void initData() {
        String url = "https://wanandroid.com/wxarticle/list/408/1/json";
        HttpUtil.sendRequest(url, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                JSONObject jsonObject = JSON.parseObject(response.body().string());
                JSONArray array = jsonObject.getJSONObject("data").getJSONArray("datas");
                wxArticles = JSONObject.parseArray(array.toJSONString(), WxArticle.class);
                runOnUiThread(() -> {
                    adapter.setList(wxArticles);
                });
            }
        });

        String url_banner = "https://www.wanandroid.com/banner/json";
        HttpUtil.sendRequest(url_banner, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                JSONObject jsonObject = JSON.parseObject(response.body().string());
                JSONArray array = jsonObject.getJSONArray("data");
                bannerBeans.addAll( JSONObject.parseArray(array.toJSONString(), BannerBean.class));
                runOnUiThread(() -> {
                    imageBannerAdapter.updateData(bannerBeans);
                });
            }
        });
    }
}